from http.client import responses

import pytest
import requests

sess = requests.session()
from common.yaml_tools import YamlTools
from common.api.Base_request import BaseRequest

api_login_data = YamlTools.read_yaml_file("api\\api_login.yaml")[0]
api_query_data = YamlTools.read_yaml_file("api\\api_query.yaml")[0]
api_getSku_data = YamlTools.read_yaml_file("api\\getSku.yaml")[0]
@pytest.mark.parametrize("logindata",api_login_data["parametrize"])
# def test_login_001(logindata):
#     url = "http://192.168.1.37:8080"+api_login_data["request"]["url"]
#     data = logindata["data"]
#     response = sess.request(api_login_data["request"]["method"],url,data=data)
#     assert_text = response.json()['message']
#     assert assert_text == logindata["expect"],'登录失败'
def test_login_001(logindata):
    response = BaseRequest.excute_api_request(api_login_data["request"],logindata)
    print(response.json())
    assert_text = response.json().get("message")
    assert assert_text == logindata["expect"],"登录失败"
    # url = "http://192.168.1.37:8080"+api_login_data["request"]["url"]
    # data = logindata["data"]
    # response = sess.request(api_login_data["request"]["method"],url,data=data)
    # assert_text = response.json()['message']
    # assert assert_text == logindata["expect"],'登录失败'

@pytest.mark.parametrize("logindata", api_login_data["parametrize"])
@pytest.mark.parametrize("querydata",api_query_data["parametrize"])
def test_query_001(logindata,querydata):
    url = "http://192.168.1.37:8080"+api_login_data["request"]["url"]
    data = logindata["data"]
    response = sess.request(api_login_data["request"]["method"], url, data=data)

    urlQuery = "http://192.168.1.37:8080"+api_query_data["request"]["url"]
    params = querydata["params"]
    responseQ = sess.request(api_query_data["request"]["method"],urlQuery,params=params)

    print(responseQ.json())
    assert_textQ = responseQ.json().get("message")
    assert assert_textQ == querydata["expect"],"登录失败"

@pytest.mark.parametrize("getSkudata", api_getSku_data["parametrize"])
def test_getSku(getSkudata):
    urlSku = "http://192.168.1.37:8080" + api_getSku_data["request"]["url"]
    params = getSkudata["params"]
    responseQ = sess.request(api_getSku_data["request"]["method"], urlSku, params=params)

    print(responseQ.json())
    assert_textQ = responseQ.json().get("message")
    assert assert_textQ == getSkudata["expect"], "登录失败"